/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.front.lineup;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.front.lineup.LineupService;
import shop.services.front.lineup.bean.Lineup;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @类名称：LineupController
 * @创建人：Wzl
 * @创建时间：2018-01-03 下午16:34:06
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/front/lineup/")
public class LineupController extends FrontBaseController<Lineup> {
    private final static String LINEUP_TYPE_BIG = "big";
    private final static String LINEUP_TYPE_MIDDLE = "middle";
    private final static String LINEUP_TYPE_SMALL = "small";
    @Resource
    private LineupService lineupService;
    @Resource
    private UserService userService;

    @Override
    public Services<Lineup> getService() {
        return lineupService;
    }


    /**
     * 查询排队列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectLineupList")
    @ResponseBody
    public JSONResult selectLineupList(Lineup lineup) {
        List<Lineup> lineupList = lineupService.selectLineupList(lineup);//查询当天桌位排队列表
        List<Lineup> lineupInfoList = new ArrayList<>();
        for (Lineup oneLineup : lineupList) {
            List<Lineup> waitList = lineupService.selectFrontWait(oneLineup);
            if (waitList == null || waitList.size() == 0) {//判断是否第一位排号，是的话添加进集合lineupInfoList
                oneLineup.setIsNow("y");
                lineupService.update(oneLineup);
                lineupInfoList.add(oneLineup);
            }
        }
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(lineupInfoList);
        for (Lineup lineupInfo : lineupInfoList) {//给集合lineupInfoList添加排队等候人数
            List<Lineup> waitList = lineupService.selectBehindWait(lineupInfo);
            if (waitList != null && waitList.size() > 0) {
                lineupInfo.setWaitAmount(waitList.size());
            } else {
                lineupInfo.setWaitAmount(0);
            }
        }
        return jsonResult;
    }

    /**
     * 查询用户排队号数
     *
     * @param lineup
     * @return
     */
    @RequestMapping(value = "selectMyLineup")
    @ResponseBody
    public JSONResult selectMyLineup(HttpSession session, Lineup lineup) {
        User user = (User) session.getAttribute("userInfo");
        lineup.setUserID(user.getId());
        lineup = lineupService.selectOne(lineup);
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(lineup);
        return jsonResult;
    }

    /**
     * 排队取号
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertLineup", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertLineup(HttpSession session, Lineup lineup) {
        User user = (User) session.getAttribute("userInfo");
        JSONResult jsonResult = new JSONResult();
        lineup.setUserID(user.getId());
        Lineup lineupNow=lineupService.selectLineupNow(lineup);
        Lineup lastLineUp=new Lineup();
        int length=0;
        int lineupID=0;
        List<Lineup> waitList = lineupService.selectBehindWait(lineup);
        if(waitList.size()>0&&waitList!=null){
            lastLineUp = waitList.get(waitList.size() - 1);
            length = String.valueOf(waitList.size()).length();
        }
        switch (lineup.getType()) {
            case LINEUP_TYPE_BIG:
                if (length == 1) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(2));
                    lineup.setNumber("A00" + String.valueOf(number + 1));
                } else if (length == 2) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(1));
                    lineup.setNumber("A0" + String.valueOf(number + 1));
                } else if (length == 3) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(0));
                    lineup.setNumber("A" + String.valueOf(number + 1));
                } else {
                    lineup.setNumber("A001");
                }
                lineupID=lineupService.insert(lineup);
                user.setLineupID(String.valueOf(lineupID));
                userService.update(user);
                break;
            case LINEUP_TYPE_MIDDLE:
                if (length == 1) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(2));
                    lineup.setNumber("B00" + String.valueOf(number + 1));
                } else if (length == 2) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(1));
                    lineup.setNumber("B0" + String.valueOf(number + 1));
                } else if (length == 3) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(0));
                    lineup.setNumber("B" + String.valueOf(number + 1));
                } else {
                    lineup.setNumber("B001");
                }
                lineupID=lineupService.insert(lineup);
                user.setLineupID(String.valueOf(lineupID));
                userService.update(user);
                break;
            case LINEUP_TYPE_SMALL:
                if (length == 1) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(2));
                    lineup.setNumber("C00" + String.valueOf(number + 1));
                } else if (length == 2) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(1));
                    lineup.setNumber("C0" + String.valueOf(number + 1));
                } else if (length == 3) {
                    int number = Integer.parseInt(lastLineUp.getNumber().substring(0));
                    lineup.setNumber("C" + String.valueOf(number + 1));
                } else {
                    lineup.setNumber("C001");
                }
                lineupID=lineupService.insert(lineup);
                user.setLineupID(String.valueOf(lineupID));
                userService.update(user);
                break;
        }
        lineup=lineupService.selectOne(lineup);
        if(lineupNow==null){
            lineup.setIsNow("y");
            lineupService.update(lineup);
        }
        return jsonResult;
    }
}
